﻿/**************************************
* Copyright by Spring Studio(http://springstudio.org)
* 树形目录解析引擎
***************************************/

/**
 * 配置参数
 */
var imagesDir = "Images/Tree/";
var menuIconDir = "Images/MenuIcon/";
var checktype = "checkbox";
var selected = null;
var icon = false;
var affect = null;

/**
* Firefox等非IE浏览器下兼容IE操纵 XMLDocument 方式
*/
if(!document.all)
{
	// 为 XMLDocument 添加 loadXML 方法
	XMLDocument.prototype.loadXML = function(xmlString)
	{
		var childNodes = this.childNodes;
		for (var i = childNodes.length - 1; i >= 0; i--)
		{
			this.removeChild(childNodes[i]);
		}
		var dp = new DOMParser();
		var newDOM = dp.parseFromString(xmlString, "text/xml");
		var newElt = this.importNode(newDOM.documentElement, true);
		this.appendChild(newElt);
	}

	// prototying the XMLDocument
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if(!xNode)
		{
			xNode = this;
		}

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =   aItems.snapshotItem(i);
		}
		return aResult;
	}

	// prototying the Element
	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else
		{
			throw "For XML Elements Only";
		}
	}

	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if(!xNode)
		{
			xNode = this;
		}

		var xItems = this.selectNodes(cXPathString, xNode);
		if(xItems.length > 0)
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	// prototying the Element
	Element.prototype.selectSingleNode = function(cXPathString)
	{
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else
		{
			throw "For XML Elements Only";
		}
	}

	// 为 Firefox 下的 Node 添加 text 属性
	Element.prototype.__defineGetter__( "text",  function()
	{
		return this.textContent;
	}
	);
}

/**
 * 准备好XML对象
 */
var treeXml = null;
var rootNode = null;

if (navigator.userAgent.indexOf("Firefox") > 0)
{
    treeXml = document.implementation.createDocument("text/xml", "", null);
}
else if (navigator.userAgent.indexOf("MSIE") > 0)
{
    treeXml = new ActiveXObject("Microsoft.XMLDOM");
    treeXml.async = false;
}

/**
 * 初始化树形目录
 */
document.write("<input type=\"hidden\" id=\"TreeXmlData\" value=\"\">");
function InitTreeXml()
{
    treeXml.loadXML(document.getElementById("TreeXmlData").value);
    rootNode = treeXml.selectNodes("root")[0];
    rootNode.setAttribute("view", "");  // 将根节点的显示序列附加到属性
    rootNode.setAttribute("level", 0);  // 将根节点的层数附加到属性

    // 输出树形目录Html代码
    treeHtml = "{" + rootNode.nodeName + "_child}";
    GetTreeHtml(rootNode);
    document.getElementById("root_child").innerHTML = treeHtml;
}

/**
 * 获取子节点HTML代码的递归
 */
function GetTreeHtml(parentNode)
{
    // 替换当前模版位
    treeHtml = treeHtml.replace("{" + parentNode.nodeName + "_child}", GetNodeHtml(parentNode));

    var nodes = parentNode.childNodes;

    for (var i = 0; i < nodes.length; i++)
    {
        GetTreeHtml(nodes[i]);
    }

    return treeHtml;
}


/*************************************************
* 获取某个节点下所有子节点的HTML代码
*/
function GetNodeHtml(parentNode)
{
    var nodes = parentNode.childNodes;
    var nodeHtml = "";

    for (var i = 0; i < nodes.length; i++)
    {
        // 当前节点的信息，先判断是否顶节点或底节点，其中顶节点又要判断是否尾节点
        var currentType = "2"; // 列表项

        // 有子节点的列表项
        if (nodes[i].childNodes.length > 0)
        {
            currentType = "3"; // 有子节点的列表项
        }

        // 顶节点
        if (i == 0)
        {
            if (nodes[i].nodeName == rootNode.nodeName)
            {
                currentType = "9"; // 根节点
            }
            else
            {
                if (nodes[i].childNodes.length > 0)
                {
                    currentType = "4"; // 顶节点
                }
                else
                {
                    currentType = "6";  // 无子节点的顶节点
                }
            }
        }

        if (i == nodes.length - 1)
        {
            if (nodes[i].childNodes.length > 0)
            {
                currentType = "5"; // 尾节点
            }
            else
            {
                currentType = "7";  // 无子节点的尾节点
            }
        }

        // 节点显示序列字符串
        var currentView = GetNodeView(parentNode.getAttribute("view"), currentType);
        nodes[i].setAttribute("view", currentView); // 将当前显示序列附加到当前节点属性
        nodes[i].setAttribute("level", parseInt(parentNode.getAttribute("level")) + 1); // 将层数附加到当前节点属性

        // ===========================================
        // 输出节点的Html代码
        nodeHtml += "<div id=\"" + nodes[i].nodeName + "\" parent=\"" + parentNode.nodeName + "\"> \n";
        nodeHtml += "	<table> \n";
        nodeHtml += "		<tr height=\"20\"> \n";

        // 根据显示序列输出HTML
        for (var j = 0; j < currentView.length; j++)
        {
            // 输出层次线
            switch (currentView.substring(j, j + 1))
            {
                case "0":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "blank.gif\"></td> \n";
                    break;

                case "1":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "list.gif\"></td> \n";
                    break;

                case "2":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_body_nochild.gif\"></td> \n";
                    break;

                case "3":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_body_plus.gif\" id=\"" + nodes[i].nodeName + "_image\" onclick=\"javascript:NodeClick(this,'" + nodes[i].nodeName + "');\" style=\"cursor: pointer\"></td> \n";
                    break;

                case "4":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_top_plus.gif\" id=\"" + nodes[i].nodeName + "_image\" onclick=\"javascript:NodeClick(this,'" + nodes[i].nodeName + "');\" style=\"cursor: pointer\"></td> \n";
                    break;

                case "5":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_bottom_plus.gif\" id=\"" + nodes[i].nodeName + "_image\" onclick=\"javascript:NodeClick(this,'" + nodes[i].nodeName + "');\" style=\"cursor: pointer\"></td> \n";
                    break;

                case "6":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_top_nochild.gif\"></td> \n";
                    break;

                case "7":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_bottom_nochild.gif\"></td> \n";
                    break;

                case "9":
                    nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + imagesDir + "node_root_plus.gif\" id=\"" + nodes[i].nodeName + "_image\" onclick=\"javascript:NodeClick(this,'" + nodes[i].nodeName + "');\" style=\"cursor: pointer\"></td> \n";
                    break;
            }
        }

        // 输出选项框
        if (selected != null)
        {
            nodeHtml += "\t\t\t<td width=\"20\"><input type=\"" + checktype +"\" id=\"check_" + nodes[i].nodeName + "\" name=\"check\"";
            if (selected.indexOf(nodes[i].getAttribute("id")) > -1)
            {
                nodeHtml += " checked";
            }
            nodeHtml += " value=\"" + nodes[i].getAttribute("id") + "\"></td> \n";
        }

        // 输出图标
        if (icon == true && nodes[i].getAttribute("icon") != "")
        {
            nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + menuIconDir + nodes[i].getAttribute("icon") + "\" width=\"20px\" height=\"20px\"></td> \n";
        }
        else if (icon == true)
        {
            nodeHtml += "\t\t\t<td width=\"20\"><img src=\"" + menuIconDir + "default.gif" + "\" width=\"20px\" height=\"20px\"></td> \n";
        }

        // 输出文本
        if (affect == "menutree") {
            if (nodes[i].getAttribute("url") != "" && selected == null) {
                nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" onclick=\"javascript:parent.document.getElementById('main').src='" + nodes[i].getAttribute("url") + "';\" style=\"white-space:nowrap;cursor:pointer;color:#000099;\">" + nodes[i].getAttribute("desc") + "</td> \n";
            }
            else if (nodes[i].childNodes.length > 0) {
                nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" onclick=\"javascript:NodeClick(document.getElementById('" + nodes[i].nodeName + "_image'),'" + nodes[i].nodeName + "');\" style=\"white-space:nowrap;cursor:pointer;\">" + nodes[i].getAttribute("desc") + "</td> \n";
            }
            else {
                nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\">" + nodes[i].getAttribute("desc") + "</td> \n";
            }
        }
        else if (affect == "catetree") {
            nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;cursor:pointer;\" onmouseover=\"javascript:this.style.backgroundColor='#fcfcfc';document.getElementById('lblOption_" + nodes[i].nodeName + "').style.visibility='visible';\" onmouseout=\"javascript:this.style.backgroundColor='';document.getElementById('lblOption_" + nodes[i].nodeName + "').style.visibility='hidden';\">";
            nodeHtml += "<span style=\"color:#000099;\" onclick=\"javascript:document.getElementById('dictframe').src='DictList.aspx?CateGuid=" + nodes[i].getAttribute("id") + "';\" title=\"查看该索引的字典列表\">" + nodes[i].getAttribute("desc") + "</span>";
            nodeHtml += "<span id=\"lblOption_" + nodes[i].nodeName + "\" style=\"visibility: hidden; padding: 0px 0px 0px 5px;\">";
            nodeHtml += "<img style=\"width: 20px; height: 20px; cursor: pointer; vertical-align: middle;\" src=\"../Images/add.gif\" alt=\"增加\" title=\"增加子项\" onclick=\"javascript:OpenAdd('" + nodes[i].getAttribute("id") + "');\">";
            nodeHtml += "<img style=\"width: 20px; height: 20px; cursor: pointer; vertical-align: middle;\" src=\"../Images/edit.gif\" alt=\"修改\" title=\"修改索引\" onclick=\"javascript:OpenEdit('" + nodes[i].getAttribute("id") + "');\">";
            nodeHtml += "<img style=\"width: 20px; height: 20px; cursor: pointer; vertical-align: middle;\" src=\"../Images/delete.gif\" alt=\"删除\" title=\"删除索引\" onclick=\"javascript:OpenDelete('" + nodes[i].getAttribute("id") + "');\">";
            nodeHtml += "</span></td> \n";
        }
        else if (affect == "dicttree") {
            nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;cursor:pointer;\" onmouseover=\"javascript:this.style.backgroundColor='#fcfcfc';document.getElementById('lblOption_" + nodes[i].nodeName + "').style.visibility='visible';\" onmouseout=\"javascript:this.style.backgroundColor='';document.getElementById('lblOption_" + nodes[i].nodeName + "').style.visibility='hidden';\">";
            nodeHtml += "<span>" + nodes[i].getAttribute("desc") + "</span>";
            nodeHtml += "<span id=\"lblOption_" + nodes[i].nodeName + "\" style=\"visibility: hidden; padding: 0px 0px 0px 5px;\">";
            nodeHtml += "<img style=\"width: 20px; height: 20px; cursor: pointer; vertical-align: middle;\" src=\"../Images/add.gif\" alt=\"增加\" title=\"增加子项\" onclick=\"javascript:OpenAdd('" + nodes[i].getAttribute("id") + "');\">";
            nodeHtml += "<img style=\"width: 20px; height: 20px; cursor: pointer; vertical-align: middle;\" src=\"../Images/edit.gif\" alt=\"修改\" title=\"修改字典\" onclick=\"javascript:OpenEdit('" + nodes[i].getAttribute("id") + "');\">";
            nodeHtml += "<img style=\"width: 20px; height: 20px; cursor: pointer; vertical-align: middle;\" src=\"../Images/delete.gif\" alt=\"删除\" title=\"删除字典\" onclick=\"javascript:OpenDelete('" + nodes[i].getAttribute("id") + "');\">";
            nodeHtml += "</span></td> \n";
        }
        else if (affect == "dictselect") {
            if (multiselect != undefined && multiselect != "") {
                nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\"><label id=\"lbl_" + nodes[i].getAttribute("id") + "\" for=\"check_" + nodes[i].nodeName + "\">" + nodes[i].getAttribute("desc") + "</label></td> \n";
            }
            else if (level != undefined && level != "") {
                if (nodes[i].getAttribute("level") == level) {
                    nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\"><label style=\"border:solid 1px #cfcfcf;cursor:pointer;padding:0px 0px 0px 2px;\" onmouseover=\"javascript:this.style.backgroundColor='#cfcfcf';\" onmouseout=\"javascript:this.style.backgroundColor='';\" onclick=\"javascript:GetValue('" + nodes[i].getAttribute("desc") + "');\" for=\"check_" + nodes[i].nodeName + "\">" + nodes[i].getAttribute("desc") + "</label></td> \n";
                }
                else {
                    nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\"><label for=\"check_" + nodes[i].nodeName + "\">" + nodes[i].getAttribute("desc") + "</label></td> \n";
                }
            }
            else {
                nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\"><label style=\"border:solid 1px #cfcfcf;cursor:pointer;padding:0px 0px 0px 2px;\" onmouseover=\"javascript:this.style.backgroundColor='#cfcfcf';\" onmouseout=\"javascript:this.style.backgroundColor='';\" onclick=\"javascript:GetValue('" + nodes[i].getAttribute("desc") + "');\" for=\"check_" + nodes[i].nodeName + "\">" + nodes[i].getAttribute("desc") + "</label></td> \n";
            }
        }
        else if (affect == "infoselect"  || affect == "serialselect") {
            nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\"><label style=\"border:solid 1px #cfcfcf;cursor:pointer;padding:0px 0px 0px 2px;\" onmouseover=\"javascript:this.style.backgroundColor='#cfcfcf';\" onmouseout=\"javascript:this.style.backgroundColor='';\" onclick=\"javascript:GetValue('" + nodes[i].getAttribute("desc") + "');\" for=\"check_" + nodes[i].nodeName + "\">" + nodes[i].getAttribute("desc") + "</label></td> \n";
        }
        else {
            nodeHtml += "\t\t\t<td id=\"lblNode\" node=\"" + nodes[i].nodeName + "\" style=\"white-space:nowrap;\"><label for=\"check_" + nodes[i].nodeName + "\">" + nodes[i].getAttribute("desc") + "</label></td> \n";
        }

        nodeHtml += "		</tr> \n";
        nodeHtml += "	</table> \n";
        nodeHtml += "</div> \n";

        nodeHtml += "<div id=\"" + nodes[i].nodeName + "_child\" count=\"" + nodes[i].childNodes.length + "\" style=\"display: none\"> \n";
        nodeHtml += "{" + nodes[i].nodeName + "_child}"; // 输出该节点的子节点模版位
        nodeHtml += "</div> \n";
    }

    return nodeHtml;
}

/*************************************************
* 输出节点显示字串
* null：0,空白；line：1,竖线；list：2,列表项目；node_body_plus：3,三叉节点；node_top_plus：4,顶节点；node_bottom_plus：5,尾节点；
* node_top_nochild：6,无子节点的顶节点；node_bottom_nochild：7,无子节点的尾节点；node_root_plus：9,根节点；
*/
function GetNodeView(parentView, currentType)
{
    var currentView = "";

    for (var i = 0; i < parentView.length; i++)
    {
        // 假如父节点不是尾节点或空白，则继承父节点信息，输出竖线
        if (parentView.substring(i, i + 1) != "5" && parentView.substring(i, i + 1) != "0")
        {
            currentView += "1";
        }
        // 如果是尾节点或空白，则输出空白
        else
        {
            currentView += "0";
        }
    }

    currentView += currentType;

    return currentView;
}

/*************************************************
* 点击可以展开的节点
*/
function NodeClick(objNodeImage, node)
{
    // 切换 + 和 - 的图片，并控制子项可见性
    var objChild = document.getElementById(node + "_child");
    if (objNodeImage.src.indexOf("plus") >= 0)
    {
        objNodeImage.src = objNodeImage.src.replace("plus", "minus");
        objChild.style.display = "block";

        // 将下一级的子项设置为可见
        for (var i = 1; i < parseInt(objChild.count) + 1; i++)
        {
            var objChildNode = document.getElementById(node + "_" + i);
        }
    }
    else
    {
        objNodeImage.src = objNodeImage.src.replace("minus", "plus");
        objChild.style.display = "none";
    }
}

/*************************************************
* 向下递归展开所有子节点的方法
*/
function NodeAllExpand(parentNode)
{
    var objChild = document.getElementById(parentNode.nodeName + "_child");
    var objImage = document.getElementById(parentNode.nodeName + "_image");

    objChild.style.display = "block";
    if (objImage != null && objImage != undefined && objImage.src.indexOf("plus") > -1)
    {
        objImage.src = objImage.src.replace("plus", "minus");
    }

    var nodes = parentNode.childNodes;
    for (var i = 0; i < nodes.length; i++)
    {
        NodeAllExpand(nodes[i]);
    }
}

/*************************************************
* 向下递归收起所有子节点的方法
*/
function NodeAllSetBy(parentNode)
{
    var objChild = document.getElementById(parentNode.nodeName + "_child");
    var objImage = document.getElementById(parentNode.nodeName + "_image");

    objChild.style.display = "none";
    if (objImage != null && objImage != undefined && objImage.src.indexOf("minus") > -1)
    {
        objImage.src = objImage.src.replace("minus", "plus");
    }

    var nodes = parentNode.childNodes;
    for (var i = 0; i < nodes.length; i++)
    {
        NodeAllSetBy(nodes[i]);
    }
}

/*************************************************
* 收起第一层以下所有子节点的方法
*/
function NodeFirstLevelSetBy()
{
    var nodes = rootNode.childNodes;
    for (var i = 0; i < nodes.length; i++)
    {
        NodeAllSetBy(nodes[i]);
    }
}

// 全选
function SelectAll()
{
    NodeAllExpand(rootNode);
    var objCheck = document.getElementsByName("check");
    for (var i = 0; i < objCheck.length; i++)
    {
        objCheck[i].checked = true;
    }
}

// 全不选
function NoSelectAll()
{
    NodeAllExpand(rootNode);
    var objCheck = document.getElementsByName("check");
    for (var i = 0; i < objCheck.length; i++)
    {
        objCheck[i].checked = false;
    }
}

function GetReturnValue()
{
    var rtn = "";
    var objCheck = document.getElementsByName("check");
    for (var i = 0; i < objCheck.length; i++)
    {
        if (objCheck[i].checked == true)
        {
            rtn += objCheck[i].value + ",";
        }
    }
    
    if (rtn.length > 0)
    {
        rtn = rtn.substring(0, rtn.length - 1);
    }

    return rtn;
}

function GetReturnDesc() {
    var rtn = "";
    var objCheck = document.getElementsByName("check");
    for (var i = 0; i < objCheck.length; i++) {
        if (objCheck[i].checked == true) {
            rtn += document.getElementById("lbl_" + objCheck[i].value).innerText + ",";
        }
    }

    if (rtn.length > 0) {
        rtn = rtn.substring(0, rtn.length - 1);
    }

    return rtn;
}